#!/bin/bash

# expect参考 https://ld246.com/article/1587355015356
# 编译前端
echo "-----frontend build start-----"
cd ./yixin-back
npm run build
echo "------frontedn build end-------"
cd ..

echo "------copy frontend start------"
# 拷贝前端 spawn后加 bash -c用来识别文件通配符
ls
expect -c "
  spawn bash -c \"scp -r ./yixin-back/dist/*   root@124.223.203.93:/usr/share/nginx/html\"
  expect {
    \"*assword\" {set timeout 30; send \"zengjian2022__\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
  }
  expect eof"

echo "------copy frontend end------"
echo "------copy backend start------"
# 拷贝后端
ls
expect -c "
spawn bash -c \"scp -r ./yixin-backend/*   root@124.223.203.93:/usr/share/nginx/yixin-backend\"
expect {
    \"*assword\" {set timeout 30; send \"zengjian2022__\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
      }
expect eof"
echo "------copy backend end------"

echo "------start remote backend------"
# 部署后端
ls
expect -c "
spawn ssh root@124.223.203.93 -p 22 \"cd /usr/share/nginx/yixin-backend;
chmod +x ./startup.sh;
nohup ./startup.sh &\"
expect {
    \"*assword\" {set timeout 30; send \"zengjian2022__\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
      }
expect eof"
# 手动ctrl+z退出会话，而不使用ctrl+c，避免打断http server
echo "start remote backend end"
echo "deploy end!"

